<?php get_header() ?>
<?php
    $option = get_option('kkfashion_1_settings');
    $catname = get_term_by('slug',$wp_query->query_vars['grid_product_category'],'grid_product_category')->name;
    $cat = get_term_by('slug',$wp_query->query_vars['grid_product_category'],'grid_product_category')->term_id;
?>
<div class="top_block container">
<div class="content">
<div class="top_block main">
<div class="content">
    <div class="left_block sidebar">
        <div class="content">
            <?php get_sidebar() ?>
        </div>
    </div>
    <div class="right_block center">
        <div class="content">
              <h1><?php echo $catname; ?></h1>
              <?php
              wp_reset_query();
              $posts_per_page = 9;
              $page = (get_query_var('page')) ? get_query_var('page') : 1;
              query_posts(array('post_type' => 'grid_products', 'tax_query' => array(
                    array(
                        'taxonomy' => 'grid_product_category',
                        'terms' => $cat,
                        'field' => 'term_id',
                    )
                ),'posts_per_page'=>$posts_per_page,'page' => $page, 'offset' => $posts_per_page*($page-1)));
              while (have_posts()): the_post(); ?>
              <div class="short-box promo">
                <a rel="prettyPhoto" href="<?php echo wp_get_attachment_url(get_post_thumbnail_id()) ?>"><?php the_post_thumbnail(array(245,245)); ?></a>
              </div>
             <?php endwhile; ?>
             <?php
              global $wp_query;
              $pagination = paginate_links( array(
                  'current' =>  max( 1,get_query_var('page')),
                  'total' => $wp_query->max_num_pages,
                  'type' => 'array',
                  'next_text' => '»',
                  'prev_text' => '«'
              ) );
              ?>
              <?php if(isset($pagination)){ 
              $pagination = str_replace("href=''", "href='".get_category_link('')."?page=1'", $pagination);
              $pagination = str_replace('href=""', 'href="'.get_category_link('').'?page=1"', $pagination);
              ?>
            <div class="pagination">
              <ul class="page-numbers">
              <?php $i=0; foreach($pagination as $page){ ?>
                  <li>
                      <?php echo $page; ?>
                  </li>
              <?php } ?>
               </ul>
            </div>
              <?php } ?>
        </div>
    </div>
</div>
<?php get_footer() ?>